<?php

namespace common\repositories\official;

use common\entities\official\DirEntity;
use Yii;
use common\repositories\BaseRepository;
use common\entities\official\BespeakEntity;
use common\entities\official\ArticleEntity;


class SubscribeRepository extends BaseRepository {
    
    public static function saveBepeak ($data) {
        $entity = new BespeakEntity();
        $entity->source = $data['source'];
        $entity->tel = $data['tel'];
        $entity->name = $data['name'];
        $entity->city = $data['city'];
        $entity->company = $data['company'];
        $entity->created_at = time();
        $entity->save();
    }
    
    /**
     * 获取新闻文章
     * @param type $offset
     * @param type $limit
     */
    public static function getArticleList ($offset, $limit, $dir) {
        $data = ArticleEntity::find()
            ->where(['del' => 0]);
        $data->andWhere(['dir_id' => $dir]);
        $data->andWhere(['status' => 1])
            ->orderBy(['modified_time' => SORT_DESC])
            ->offset((int)$offset* $limit)
            ->limit((int)$limit);
        $data = $data->asArray()->all();
        if (!empty($data)){
            foreach ($data as $k => $v) {
                $data[$k]['content'] = htmlspecialchars_decode($v['content']);
            }
        }
        
        return $data;   
    }

    /**
     * 获取目录列表
     */
    public static function getDirList() {
        $date = DirEntity::find()
            ->where(['status'=>1])
            ->orderBy(['sort' => SORT_DESC])
            ->asArray()
            ->all();
        return $date;
    }
    
    /**
     * 获取文章详细信息
     * @param type $id
     */
    public static function getArticle ($id, $status=0, $type=null,$dir=0) {
        $data = ArticleEntity::find()
            ->where(['a_id' => $id]);


        if ($status != 0) {
            $data->andWhere(['del' => 0])->where(['status' => 1]);
        }
        
        if ($type == 'b') {
            $data->andWhere(['>', 'a_id', $id]);
            $data->andWhere([ 'dir_id'=> $dir]);
            $data->andWhere(['del' => 0]);
            $data->orderBy(['a_id'=>SORT_ASC]);
        } elseif ($type == 's') {
            $data->andWhere(['<', 'a_id', $id]);
            $data->andWhere([ 'dir_id'=> $dir]);
            $data->andWhere(['del' => 0]);
            $data->orderBy(['a_id'=>SORT_DESC]);
        }

        $data = $data->asArray()->one();
        !empty($data) && $data['content'] = htmlspecialchars_decode($data['content']);
        return $data;
    }

    
    /**
     * 获取文章总数
     * @param type $dir 分类
     * @return type
     */
    public static function getArticleCount($dir) {
        $data =  ArticleEntity::find()
            ->where(['del' => 0]);
        $data->andWhere(['status' => 1]);
        $data = $data->andWhere(['dir_id' => $dir])
            ->count();
        return $data;
    }
    
    /**
     * 获取预约列表
     * @param type $offset 页码
     * @param type $limit 每页条数
     * @param type $search 搜索关键字
     * @return type
     */
    public static function getBespeakList ($offset, $limit, $search) {
        $offset--;
        $data = BespeakEntity::find();
        if (!empty($search)) {
            $data->where(['like', 'name', $search])->orwhere(['like', 'tel', $search]);
        }
        return $data->orderBy(['created_at' => SORT_DESC])
            ->offset((int)$offset* $limit)
            ->limit((int)$limit)     
            ->all();
    }

    /**
     * 根据id查询
     * @param $id
     * @return array|\yii\db\ActiveRecord[]
     */
    public static function getBespeakListByIds($id)
    {
        $data = BespeakEntity::find();
        $data->where(['in', 'id', explode(',',$id)]);
        return $data->orderBy(['id'=>SORT_DESC])->asArray()->all();
    }

}